package com.jeesite.modules.fl.web;


import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.fl.entity.Clinic;
import com.jeesite.modules.fl.entity.SysHoliday;
import com.jeesite.modules.fl.service.ClinicService;
import com.jeesite.modules.fl.service.SysHolidayService;
import com.jeesite.modules.sys.entity.DictData;

@Controller
@RequestMapping(value = "${adminPath}/fl/holiday")
public class SysHolidayController extends BaseController{

	@Autowired
	private SysHolidayService holidayService;
	@Autowired
	private ClinicService clinicService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public SysHoliday get(String id, boolean isNewRecord) {
		return holidayService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("fl:holiday:view")
	@RequestMapping(value = {"list", ""})
	public String list(SysHoliday holiday, HttpServletRequest request, HttpServletResponse response, Model model) {
		model.addAttribute("holiday", holiday);
		Clinic entity = new Clinic();
		entity.setStatus("0");
		List<Clinic> clinicList = clinicService.findList(entity);
		List<DictData> dicts = new ArrayList<DictData>();
        for(Clinic c:clinicList){
        	DictData data = new DictData();
        	data.setDictLabelOrig(c.getName());
        	data.setDictValue(c.getId());
        	dicts.add(data);
        }
		model.addAttribute("clinicList", dicts);
		return "modules/fl/holidayList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("fl:holiday:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<SysHoliday> listData(SysHoliday holiday, HttpServletRequest request, HttpServletResponse response) {
		Page<SysHoliday> page = holidayService.findPage(new Page<SysHoliday>(request, response), holiday);
		return page;
	}
	
	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("fl:holiday:view")
	@RequestMapping(value = "form")
	public String form(SysHoliday holiday, Model model) {
		model.addAttribute("holiday", holiday);
		Clinic entity = new Clinic();
        entity.setStatus("0");
        List<Clinic> clinicList = clinicService.findList(entity);
        model.addAttribute("clinicList", clinicList);
		return "modules/fl/holidayForm";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("fl:holiday:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated SysHoliday holiday, HttpServletRequest request) {
		holidayService.save(holiday);
		return renderResult(Global.TRUE, text("保存数据成功！"));
	}
	
	/**
	 * 停用数据
	 */
	@RequiresPermissions("fl:holiday:edit")
	@RequestMapping(value = "disable")
	@ResponseBody
	public String disable(SysHoliday holiday) {
		holiday.setStatus(SysHoliday.STATUS_DISABLE);
		holidayService.updateStatus(holiday);
		return renderResult(Global.TRUE, text("停用数据成功"));
	}
	
	/**
	 * 启用数据
	 */
	@RequiresPermissions("fl:holiday:edit")
	@RequestMapping(value = "enable")
	@ResponseBody
	public String enable(SysHoliday holiday) {
		holiday.setStatus(SysHoliday.STATUS_NORMAL);
		holidayService.updateStatus(holiday);
		return renderResult(Global.TRUE, text("启用数据成功"));
	}
	
	/**
	 * 删除数据
	 */
	@RequiresPermissions("fl:holiday:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(SysHoliday holiday) {
		holidayService.delete(holiday);
		return renderResult(Global.TRUE, text("删除数据成功！"));
	}
}
